package org.lgiavedoni.android.franq.core.communication.imp.phone;

import java.util.Date;

import org.lgiavedoni.android.franq.core.communication.CommunicationSource;
import org.lgiavedoni.android.franq.core.communication.CommunicationType;
import org.lgiavedoni.android.franq.core.message.MessageOut;
import org.lgiavedoni.android.franq.core.place.Place;
import org.lgiavedoni.android.franq.core.place.PlaceManager;
import org.lgiavedoni.android.franq.service.config.ConfigService;
import org.lgiavedoni.android.franq.service.config.ConfigService.ConfigKey;
import org.lgiavedoni.android.franq.util.date.DateUtil;
import org.lgiavedoni.android.franq.util.phone.SpeechUtil;

public class CommunicationPhoneSpeech implements CommunicationSource {

	public static final CommunicationPhoneSpeech inst = new CommunicationPhoneSpeech();

	private Date silenceTill;

	private CommunicationPhoneSpeech() {
	}

	@Override
	public void sendMessage(MessageOut message) {
		Place currentPlace = PlaceManager.inst != null ? PlaceManager.inst.getCurrentPlace() : null;
		// Check the config
		if (ConfigService.inst.getValueAsBoolean(ConfigKey.FRANK_SILENCE))
			return;
		// Check the place
		if (currentPlace != null) {
			if (currentPlace.isSpeakHere())
				SpeechUtil.inst.speech(message.getText(CommunicationType.chat));
		} else {
			// We don't have any place at the moment
			if (silenceTill != null) {
				if (silenceTill.getTime() < DateUtil.getTimeInMillis()) {
					// we can speak again
					silenceTill = null;
					SpeechUtil.inst.speech(message.getText(CommunicationType.chat));
				}
			} else {
				SpeechUtil.inst.speech(message.getText(CommunicationType.chat));
			}
		}
	}

	public void silencePlease() {
		silenceTill = new Date(DateUtil.getTimeInMillis() + DateUtil.getMillisecondsOfHours(1));
	}

	public void nowYouCanSpeakAgain() {
		silenceTill = null;
	}

}
